<template>
  <el-dialog
    title="收货地址"
    :title="options.title"
    :visible.sync="options.visible"
    width="30%">
    <el-form :model="form" :rules="rules" ref="form">
      <el-form-item label="名称" :label-width="formLabelWidth" prop="name">
        <el-input v-model="form.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="类型" :label-width="formLabelWidth" prop="type">
        <!-- <el-input v-model="form.type" autocomplete="off"></el-input> -->
        <!-- <el-cascader
          v-model="fileType"
          :options="fileOptions"
          @change="handleChange"
          style="width: 100%">
        </el-cascader> -->

        <el-select v-model="form.type" placeholder="请选择" style="width: 100%">
          <el-option
            v-for="item in fileOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item
        label="数据长度"
        :label-width="formLabelWidth"
        prop="dataLength">
        <el-input v-model="form.dataLength" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item
        label="帧头"
        :label-width="formLabelWidth"
        prop="frameHeader">
        <el-input v-model="form.frameHeader" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item
        label="帧尾"
        :label-width="formLabelWidth"
        prop="endOfFrame">
        <el-input v-model="form.endOfFrame" autocomplete="off"></el-input>
      </el-form-item> -->
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="confirm"> 确 定 </el-button>
    </div>
  </el-dialog>
</template>

<script>
  export default {
    props: {
      options: {
        type: Object,
        default() {
          return {};
        },
      },
      row: {
        type: Object,
        default() {
          return null;
        },
      },
    },
    data() {
      return {
        tagItem: [
          { id: 2, name: "test1" },
          { id: 3, name: "test2" },
        ],
        form: {
          // 描述文件类型
          type: null,
        },
        rules: {
          name: [{ required: true, message: "请输入名称", trigger: "blur" }],
          type: [{ required: true, message: "请选择类型", trigger: "blur" }],
          // dataLength: [
          //   { required: true, message: "请输入数据长度", trigger: "blur" },
          // ],
          // frameHeader: [
          //   { required: true, message: "请输入帧头", trigger: "blur" },
          // ],
          // endOfFrame: [
          //   { required: true, message: "请输入帧尾", trigger: "blur" },
          // ],

          // path: [
          //   { required: true, message: "请输入路径", trigger: "blur" },
          //   // {
          //   //   min: 1,
          //   //   max: 18,
          //   //   message: "长度在 1 到 18 个字符",
          //   //   trigger: "blur",
          //   // },
          // ],
          // type: [
          //   {
          //     required: true,
          //     message: "请至少选择一个类型",
          //     trigger: "change",
          //   },
          // ],
        },
        formLabelWidth: "100px",
        filePath: "",
        filetext: "",
        // 描述文件类型数据
        fileOptions: [
          { value: 1, label: "MetaA" },
          { value: 2, label: "MetaB" },
        ],
      };
    },
    created() {
      this.init();
    },
    methods: {
      init() {
        if (this.row) {
          this.form = JSON.parse(JSON.stringify(this.row));
          console.log("🚀 ~ init ~ this.form:", this.form);
        }
        this.getTagAll();
      },
      //查询所有标签
      getTagAll() {
        let result = window.iTag.getAll();
        let res = JSON.parse(result);
        this.tagItem = res.data;
      },
      selectFile() {
        this.$refs.fileInput.click();
      },
      handleFileUpload(event) {
        const file = event.target.files[0];
        if (!file) {
          alert("请选择一个文件");
          return;
        }
        this.filePath = file.name;
        console.log("🚀 ~ handleFileUpload ~ this.filePath:", this.filePath);
        let result = window.iOperator.upload(this.filePath);
        let res = JSON.parse(result);
        this.filetext = res.data;
        this.form.path = res.data;
        // if (code == 1) {
        //   this.$message.success(res.msg);
        // } else {
        //   this.$message.error(res.msg);
        // }
        this.$refs.fileInput.value = null;

        // 检查文件类型是否为 JSON
        // const fileExtension = file.name.split(".").pop().toLowerCase();
        // if (fileExtension !== "json") {
        //   this.$message.error("只允许上传JSON格式的文件");
        //   return;
        // } else {
        //   this.filePath = file.name;
        //   this.form.path = 111;
        //   console.log("🚀 ~ handleFileUpload ~ this.filePath:", this.filePath);
        //   // const fileName = file.name.split("\\").pop();
        //   // this.fileName = fileName.split(".")[0];
        //   // let item = {
        //   //   // key: this.fileName2,
        //   //   key: "MetaB",
        //   //   path: this.filePath2,
        //   // };
        //   // let itemAdd = JSON.stringify(item);
        //   // let result = window.iTest.upload(itemAdd);
        //   // let res = JSON.parse(result);
        //   // if (res.code == 1) {
        //   //   this.$message.success(res.msg);
        //   // } else {
        //   //   this.$message.error(res.msg);
        //   // }
        //   // this.$refs.fileInput2.value = null;
        // }
      },

      confirm() {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.row
              ? this.$emit("onEditClose", this.form)
              : this.$emit("onAddClose", this.form);
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
      close() {
        this.row ? this.$emit("onEditClose") : this.$emit("onAddClose");
      },
      handleChange(value) {
        console.log(value);
      },
    },
  };
</script>

<style scoped>
  :deep(.el-dialog) {
    padding-right: 30px;
    border-radius: 15px;
  }
</style>
